package service

import (
	__ "class-server/basic/proto"
	"class-server/handler/models"
	"context"
	"fmt"
)

// server is used to implement helloworld.GreeterServer.
type Server struct {
	__.UnimplementedClassServer
}

// SayHello implements helloworld.GreeterServer
func (s *Server) Login(_ context.Context, in *__.LoginReq) (*__.LoginResp, error) {
	var user models.User
	user.Username = in.Username
	user.Password = in.Password
	if err := user.GetUserByUsername(); err != nil {
		return nil, err
	}
	if user.Id == 0 {
		return nil, fmt.Errorf("该用户不存在，禁止登录")
	}
	if user.Password != in.Password {
		return nil, fmt.Errorf("密码错误，请重新输入")
	}
	return &__.LoginResp{
		Id: int64(user.Id),
	}, nil
}

// 类型展示
func (s *Server) GetTypesList(_ context.Context, in *__.GetTypesListReq) (*__.GetTypesListResp, error) {
	var typesLists []*__.TypesList
	var tl models.Types
	list, err := tl.GetTypesList()
	if err != nil {
		return nil, fmt.Errorf("课程分类展示失败")
	}
	for _, types := range list {
		typesLists = append(typesLists, &__.TypesList{
			TypesName: types.TypesName,
			TypesImg:  types.TypesImg,
			Nums:      int64(types.Nums),
			Id:        int64(types.Id),
		})
	}
	return &__.GetTypesListResp{
		List: typesLists,
	}, nil
}

// 课程分类展示
func (s *Server) GetCLassList(_ context.Context, in *__.GetCLassListReq) (*__.GetCLassListResp, error) {
	var classLists []*__.ClassList
	var cl models.Classes
	list, err := cl.GetClassList(int(in.Page), int(in.PageSIze))
	if err != nil {
		return nil, fmt.Errorf("课程分页展示失败")
	}
	for _, class := range list {
		classLists = append(classLists, &__.ClassList{
			ClassName:  class.ClassName,
			Author:     class.Author,
			ClassImg:   class.ClassImg,
			ClassUrl:   class.ClassUrl,
			ClassPrice: int64(class.ClassPrice),
			VipPrice:   int64(class.VipPrice),
			TypesId:    int64(class.TypesId),
		})
	}
	return &__.GetCLassListResp{
		List: classLists,
	}, nil
}

// 课程详情
func (s *Server) GetClassDetail(_ context.Context, in *__.GetClassDetailReq) (*__.GetClassDetailResp, error) {
	var class models.Classes
	class.BuyStatus = uint(in.BuyStatus)
	if in.BuyStatus == 2 {
		return nil, fmt.Errorf("该课程未购买，仅支持第一章节进行试看")
	}
	if err := class.GetClassDetail(); err != nil {
		return nil, fmt.Errorf("课程详情展示失败")
	}

	return &__.GetClassDetailResp{
		ClassName:  class.ClassName,
		Author:     class.Author,
		ClassImg:   class.ClassImg,
		ClassUrl:   class.ClassUrl,
		ClassPrice: int64(class.ClassPrice),
		VipPrice:   int64(class.VipPrice),
		TypesId:    int64(class.TypesId),
	}, nil
}

// 添加收藏
func (s *Server) CreateCollect(_ context.Context, in *__.CreateCollectReq) (*__.CreateCollectResp, error) {
	collect := models.Collect{
		UserId:    uint(in.UserId),
		ClassesId: uint(in.ClassesId),
		TypesId:   uint(in.TypesId),
		Price:     uint(in.Price),
	}
	if err := collect.Create(); err != nil {
		return nil, fmt.Errorf("添加收藏失败")
	}
	return &__.CreateCollectResp{
		Id: int64(collect.Id),
	}, nil
}

// 查看收藏
func (s *Server) GetCollectDetail(_ context.Context, in *__.GetCollectDetailReq) (*__.GetCollectDetailResp, error) {
	var collect models.Collect
	collect.Id = uint(in.Id)

	if err := collect.GetCollectDetail(); err != nil {
		return nil, fmt.Errorf("收藏展示失败")
	}

	return &__.GetCollectDetailResp{
		UserId:    int64(collect.UserId),
		ClassesId: int64(collect.ClassesId),
		TypesId:   int64(collect.TypesId),
		Price:     int64(collect.Price),
	}, nil
}
